<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表页</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/blog_list.css">
</head>
<body>
    <!--导航栏-->
     <div class="nav">
        <img id = "logo" src="img/logo.png" alt="">
        <span class="title">我的博客系统</span>
        <!--用于占位-->
        <span class="spacer"></span>
        <!--按钮-->
        <a href="blog_list.html"><img src="img/home.png" alt="">主页</a>
        <a href="blog_edit.html"><img src="img/edit.png" alt="">写博客</a>
        <a href="logout"><img src="img/zhuxiao.png" alt="">注销</a>
     </div>

     <!--页面主体-->
     <div class="container">
        <div class="container-left">
            <div class="user">
                <img src="img/profile_pic.jpg" alt="">
                <h3></h3>
                <a href="#">gitee 地址</a>
                <div class="counter">
                    <a id="articleA">文章</a>
                    <a id="followingA">关注</a>
                    <a href="collection_list.html">收藏</a>
                </div>
                <div class="counter">
                    <span id="blogCount"></span>
                    <span id="following"></span>
                    <span id="collection"></span>
                </div>
            </div>
        </div>
        <div class="container-right">
            <!-- <div class="blog">
                <div class="title">我的第一篇博客</div>
                <div class="date">2023-11-30 12 :00:00</div>
                <div class="article">Lorem ipsum dolor sit amet consectetur, adipisicing elit. Dignissimos in perspiciatis nesciunt esse earum, ullam illum excepturi obcaecati, asperiores quas assumenda error pariatur! Reprehenderit, cum repudiandae enim veritatis error vitae?</div>
                <a href="#">查看全文 >></a>
            </div> -->
        </div>
     </div>
     <script src="js/jquery.min.js"></script>
     <script src="js/app.js"></script>
     <script>
        function getBlogs() {
            $.ajax({
                type : 'get',
                url : 'blog',
                success : function(body) {
                    let container = document.querySelector('.container-right');
                    for(let blog of body) {
                        let blogDiv = document.createElement('div');
                        blogDiv.className = "blog";
                        //创建title
                        let titleDiv = document.createElement('div');
                        titleDiv.className = "title";
                        titleDiv.innerHTML = blog.title;
                        blogDiv.appendChild(titleDiv);
                        //创建date
                        let dateDiv = document.createElement('div');
                        dateDiv.className = "date";
                        dateDiv.innerHTML = blog.postTime;
                        blogDiv.appendChild(dateDiv);
                        //创建article
                        let articleDiv = document.createElement('div');
                        articleDiv.className = "article";
                        articleDiv.innerHTML = blog.content;
                        blogDiv.appendChild(articleDiv);
                        //创建查看全文按钮
                        let a = document.createElement('a');
                        a.innerHTML = "查看全文 >>"
                        a.href = "blog_detail.html?blogId=" + blog.blogId;
                        blogDiv.appendChild(a);
                        //把blogDiv加入外层元素
                        container.appendChild(blogDiv);
                    }
                }
            })
        }
        getBlogs();
        checkLogin();
        
        function getUserInfo() {
            $.ajax({
                type:'get',
                url : 'userInfo',
                success : function(body) {
                    let h3 = document.querySelector('.user h3');
                    h3.innerHTML = body.username;
                    let giteeA = document.querySelector('.user a');
                    if(body.gitUrl != null) {
                        giteeA.href = body.gitUrl;
                    }
                    //更新博客数量
                    let blogCount = document.querySelector('#blogCount');
                    blogCount.innerHTML=body.blogCount;
                    //更新关注数量
                    let following = document.querySelector('#following');
                    following.innerHTML=body.followingCount;
                    //更新收藏数量
                    let collection = document.querySelector('#collection');
                    collection.innerHTML=body.collectionCount;
                    //给关注添加跳转链接
                    let followingA = document.querySelector('#followingA');
                    followingA.href = "following_list.html?userId=" + body.userId;
                    //给文章添加跳转链接
                    let articleA = document.querySelector('#articleA');
                    articleA.href = 'user_bloglist.html?userId='+body.userId;
                }
            })
        }
        getUserInfo();
     </script>
</body>
</html>